// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-7-7 13:08:27
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   deal_html_input.java

package com.creawor.imei.util;

import com.creawor.imei.base.AbsQueryMap;
import com.creawor.imei.base.Common_qry_QueryMap;
import java.io.PrintStream;
import java.util.*;
import net.sf.hibernate.HibernateException;

public class deal_html_input extends AbsQueryMap
{

    public deal_html_input()
        throws HibernateException
    {
        initSession();
    }

    public String getHtmlType1(String tableName, String fieldName)
    {
        String s = "";
        ArrayList al = new ArrayList();
        try
        {
            Common_qry_QueryMap query_map = new Common_qry_QueryMap();
            String SQLstr = (new StringBuilder("select html_type,select_para from cicf_field_info where table_name='")).append(tableName).append("' and field_name='").append(fieldName).append("'").toString();
            al = query_map.findAll_sql(SQLstr);
        }
        catch(Exception e1)
        {
            e1.printStackTrace();
        }
        if(al != null)
            try
            {
                if(al.iterator().hasNext())
                    s = (String)((HashMap)al.iterator().next()).get("html_type");
            }
            catch(Exception e)
            {
                s = "";
                e.printStackTrace();
            }
        return s;
    }

    public String getSelectParam(String tableName, String fieldName)
    {
        String s = "";
        ArrayList al = new ArrayList();
        try
        {
            Common_qry_QueryMap query_map = new Common_qry_QueryMap();
            String SQLstr = (new StringBuilder("select html_type,select_para from cicf_field_info where table_name='")).append(tableName).append("' and field_name='").append(fieldName).append("'").toString();
            al = query_map.findAll_sql(SQLstr);
        }
        catch(HibernateException e1)
        {
            e1.printStackTrace();
        }
        if(al != null)
            try
            {
                if(al.iterator().hasNext())
                    s = (String)((HashMap)al.iterator().next()).get("select_para");
            }
            catch(Exception e)
            {
                s = "";
                e.printStackTrace();
            }
        return s;
    }

    public ArrayList getOptionList(String select_sql_param)
    {
        ArrayList al = new ArrayList();
        if(select_sql_param.trim().toLowerCase().indexOf("select") == 0)
        {
            try
            {
                Common_qry_QueryMap query_map = new Common_qry_QueryMap();
                al = query_map.findAll_sql(select_sql_param);
            }
            catch(Exception e1)
            {
                e1.printStackTrace();
            }
        } else
        {
            String sl[] = select_sql_param.split(",");
            int i = sl.length;
//            System.out.println(i);
            for(int j = 0; j < i; j++)
            {
//                System.out.println(sl[j]);
                String sl_temp[] = sl[j].split(":");
                HashMap hm = new HashMap();
//                System.out.println(sl_temp.length);
                if(sl_temp.length == 2)
                {
                    hm.put("code", sl_temp[0]);
                    hm.put("name", sl_temp[1]);
                } else
                {
                    hm.put("code", sl_temp[0]);
                    hm.put("name", sl_temp[0]);
                }
                al.add(hm);
            }

        }
        return al;
    }

    public String getSelectBox(String fieldName, String old_value, String select_param)
    {
        String s = (new StringBuilder("<select name='")).append(fieldName).append("'>\n").toString();
        Iterator it = getOptionList(select_param).iterator();
        HashMap ht;
        for(s = (new StringBuilder(String.valueOf(s))).append("<option value=''></option>").toString(); it.hasNext(); s = (new StringBuilder(String.valueOf(s))).append(">").append((String)ht.get("name")).append("</option>").toString())
        {
            ht = (HashMap)it.next();
//            System.out.println((String)ht.get("code"));
//            System.out.println((String)ht.get("name"));
            s = (new StringBuilder(String.valueOf(s))).append("<option value='").append((String)ht.get("code")).append("' ").toString();
            if(ht.get("code").equals(old_value))
                s = (new StringBuilder(String.valueOf(s))).append(" selected ").toString();
        }

        s = (new StringBuilder(String.valueOf(s))).append("\n</select>\n").toString();
        return s;
    }
    
    public String getSelectBoxForId(String fieldName, String old_value, String select_param,String id,String method)
    {
        String s = (new StringBuilder("<select onchange='"+method+"' id='"+id+"' name='")).append(fieldName).append("'>\n").toString();
        Iterator it = getOptionList(select_param).iterator();
        HashMap ht;
        for(s = (new StringBuilder(String.valueOf(s))).append("<option value=''></option>").toString(); it.hasNext(); s = (new StringBuilder(String.valueOf(s))).append(">").append((String)ht.get("name")).append("</option>").toString())
        {
            ht = (HashMap)it.next();
//            System.out.println((String)ht.get("code"));
//            System.out.println((String)ht.get("name"));
            s = (new StringBuilder(String.valueOf(s))).append("<option value='").append((String)ht.get("code")).append("' ").toString();
            if(ht.get("code").equals(old_value))
                s = (new StringBuilder(String.valueOf(s))).append(" selected ").toString();
        }

        s = (new StringBuilder(String.valueOf(s))).append("\n</select>\n").toString();
        return s;
    }

    public String getCheckBox(String fieldName, String old_value, String select_param)
    {
        String s = "";
        for(Iterator it = getOptionList(select_param).iterator(); it.hasNext();)
        {
            HashMap ht = (HashMap)it.next();
            s = (new StringBuilder(String.valueOf(s))).append("<input type='checkbox' name='").append(fieldName).append("' value='").append((String)ht.get("code")).append("' ").toString();
            if(ht.get("code").equals(old_value))
                s = (new StringBuilder(String.valueOf(s))).append(" checked ").toString();
            s = (new StringBuilder(String.valueOf(s))).append(">").append((String)ht.get("name")).append("<br>").toString();
        }

        return s;
    }

    public String getRadioBox(String fieldName, String old_value, String select_param)
    {
        String s = "";
        for(Iterator it = getOptionList(select_param).iterator(); it.hasNext();)
        {
            HashMap ht = (HashMap)it.next();
            s = (new StringBuilder(String.valueOf(s))).append("<input type='radio' name='").append(fieldName).append("' value='").append((String)ht.get("code")).append("' ").toString();
            if(ht.get("code").equals(old_value))
                s = (new StringBuilder(String.valueOf(s))).append(" checked ").toString();
            s = (new StringBuilder(String.valueOf(s))).append(">").append((String)ht.get("name")).append("<br>").toString();
        }

        return s;
    }

    public String getTranslate(String fieldName, String old_value, String select_param)
    {
        String s = old_value;
        for(Iterator it = getOptionList(select_param).iterator(); it.hasNext();)
        {
            HashMap ht = (HashMap)it.next();
            if(ht.get("code").equals(old_value))
                s = (String)ht.get("name");
        }

        return s;
    }

    public String getTranslate(Object old_value, ArrayList select_param_list)
    {
        String s = old_value.toString();
        for(Iterator it = select_param_list.iterator(); it.hasNext();)
        {
            HashMap ht = (HashMap)it.next();
            if(ht.get("code").equals(old_value))
                s = (String)ht.get("name");
        }

        return s;
    }

    public String getInputBox(String fieldName, String old_value, String html_type, String select_param)
    {
        String s = "";
        if(html_type.equals("3"))
            s = getSelectBox(fieldName, old_value, select_param);
        if(html_type.equals("4"))
            s = getRadioBox(fieldName, old_value, select_param);
        if(html_type.equals("5"))
            s = getCheckBox(fieldName, old_value, select_param);
        return s;
    }
    
    public static String getSelectBox(String fieldName, String old_value, ArrayList<Map> sl,String valueColumn, String nameColumn)
    {
    	String str ="";
    	
    	for(Iterator it = sl.iterator();it!=null&&it.hasNext();)
	   	 {
	   	 	Map map = (Map)it.next();
	   		str += "<option  value='"+map.get(valueColumn)+"'";
	   		if(old_value.equals(""+map.get(valueColumn)))
	   		{
	   			str=str+" selected='selected' ";
	   		}
	   		str+=(">"+map.get(nameColumn)+"</option>");
//	   		System.out.println(str);
	   		
	   	}
    	
    	return str;
    }
    
    
    public static void main(String[] args) {
		ArrayList<Map> list = new ArrayList<Map>();
		
		Map mp = new HashMap();
		mp.put("id", "1");
		mp.put("name", "test1");
		
		Map mp2 = new HashMap();
		mp2.put("id", "2");
		mp2.put("name", "test2");
		list.add(mp);
		list.add(mp2);
		
		System.out.println(getSelectBox("","2",list,"id","name"));
	}
}